﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Workflow.Interface;
using MyLib;
using Workflow.Entity;
using Portal.Entity;
using Portal.Logic;

namespace Workflow
{
    public partial class NewTask : System.Web.UI.Page
    {
        IProcessCategory _pc = ServiceContainer.Instance().GetService<IProcessCategory>();
        List<ProcessEntity> _initProcessList = new List<ProcessEntity>();
        Workflow.Interface.ITask _task = ServiceContainer.Instance().GetService<Workflow.Interface.ITask>();
        string _userAccount = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            string user = Request.QueryString["user"];
            if (!string.IsNullOrEmpty(user))
            {
                _userAccount = user;
            }
            else
            {
                _userAccount = Portal.Logic.SSOLogic.GetLoginName().Replace("\\", "/");
            }
            //load init process
            _initProcessList = _task.GetInitProcessList(_userAccount);

            //bind category
            List<ProcessCategoryEntity> lists = _pc.GetList();
            ProcessCategoryEntity pe = new ProcessCategoryEntity();
            pe.CATEGORYNAME = Workflow.Resources.lang.NewTask_AllProcess;
            lists.Insert(0, pe);
            rpProcessCategory.DataSource = lists;
            rpProcessCategory.DataBind();

            rpAllProcess.DataSource = lists;
            rpAllProcess.DataBind();

            txtCurrentCategory.Text = pe.CATEGORYID;
        }

        protected void rpAllProcess_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                //load process by category
                Repeater rpControl = (Repeater)e.Item.FindControl("rpProcess");
                ProcessCategoryEntity pce = (ProcessCategoryEntity)e.Item.DataItem;
                List<ProcessEntity> lists = new List<ProcessEntity>();
                if (pce.CATEGORYNAME == Workflow.Resources.lang.NewTask_AllProcess)
                {
                    lists = _initProcessList;
                }
                else
                {
                    lists = _pc.GetProcessList(pce.CATEGORYNAME);
                }
                lists = FilterList(lists);
                rpControl.DataSource = lists;
                rpControl.DataBind();
            } 
        }

        List<ProcessEntity> FilterList(List<ProcessEntity> lists)
        {
            List<ProcessEntity> newlist = new List<ProcessEntity>();
            for (int i = 0; i < _initProcessList.Count; i++)
            {
                ProcessEntity process= lists.Find(delegate(ProcessEntity entity) {
                    if (entity.PROCESSNAME.ToUpper().Trim() == _initProcessList[i].PROCESSNAME.ToUpper().Trim())
                    {
                        entity.INITIATEID = _initProcessList[i].INITIATEID;
                        return true;
                    }
                    return false;
                });
                if (process != null)
                {
                    newlist.Add(process);
                }
            }
            return newlist;
        }

        public string GetCategoryImage(string categoryName)
        {
            ResourceLogic logic=new ResourceLogic();
            List<ResourceEntity> list = logic.GetResourceList();
            ResourceEntity ety = list.Find(p => (p.CNNAME == categoryName || p.ENNAME == categoryName) && p.TYPE.ToUpper() == "PROCESSCATEGORY");
            string pic = "lc5.jpg";
            if (ety != null)
            {
                if (!string.IsNullOrEmpty(ety.EXT01))
                {
                    pic = ety.EXT01;
                }
            }
            return categoryName == Workflow.Resources.lang.NewTask_AllProcess ? "lc1.jpg" : pic;
        }
    }
}